package org.erp.store.strategy.hook;

import org.erp.common.enums.DeviceInventoryEvent;
import org.erp.store.domain.dto.BatchDeviceInventoryDto;
import org.erp.store.domain.entity.DeviceInventory;
import org.erp.store.infrastructure.event.InventoryLogEvent;
import org.erp.store.infrastructure.event.ProductCallbackEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component("defaultAfterPersistHook")
public class DefaultAfterPersistHook implements AfterPersistHook {
    @Resource
    private ApplicationEventPublisher eventPublisher;

    @Override
    public void accept(BatchDeviceInventoryDto dto, List<DeviceInventory> updateList, Map<String, Integer> originalStateMap) {
        if (dto.getTargetEvent() != null && dto.getTargetEvent().requiresProductCallback()) {
            eventPublisher.publishEvent(new ProductCallbackEvent(dto, updateList));
        }
        eventPublisher.publishEvent(new InventoryLogEvent(updateList, dto.getTargetEvent(), originalStateMap));
    }
}
